package com.cndw.kungfu.dao;

import java.util.List;
import java.util.Map;

import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.KeyColumnOfMap;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;
import com.cndw.kungfu.domain.Player;

@DAO
public interface IPlayerDao {

	public static final String TABLE = " kf_player ";

	public static final String FIELDS = " playerId,playerName,accountName,level,gold,goldTicke,asset,vocationId,regTime,lastLogin,offLineTime,lastIp,onLineTime,vipLevel,vipScore,knightLevel,knightExpired";
	
	@SQL("SELECT "
			+ FIELDS
			+ " FROM "
			+ TABLE
			+ " where 1=1 #if(:playerId>0){and playerId=:playerId } #if(:playerName){and playerName like :playerName } #if(:accountName){and accountName like :accountName } #if(:regBeginTime>0){and regTime>:regBeginTime } #if(:regEndTime>0){and regTime<:regEndTime } #if(:loginBeginTime>0){and lastLogin>:loginBeginTime } #if(:loginEndTime>0){and lastLogin<:loginEndTime } limit :offset,:limit")
	public List<Player> getPlayers(@SQLParam("playerId") long playerId, @SQLParam("playerName") String playerName, @SQLParam("accountName") String accountName,
			@SQLParam("regBeginTime") int regBeginTime, @SQLParam("regEndTime") int regEndTime, @SQLParam("loginBeginTime") int loginBeginTime,
			@SQLParam("loginEndTime") int loginEndTime, @SQLParam("offset") int offset, @SQLParam("limit") int limit);

	@SQL("SELECT COUNT(*) FROM "
			+ TABLE
			+ " where 1=1 #if(:playerId>0){and playerId=:playerId } #if(:playerName){and playerName like :playerName } #if(:accountName){and accountName like :accountName } #if(:regBeginTime>0){and regTime>:regBeginTime } #if(:regEndTime>0){and regTime<:regEndTime } #if(:loginBeginTime>0){and lastLogin>:loginBeginTime } #if(:loginEndTime>0){and lastLogin<:loginEndTime } ")
	public int getPlayersCount(@SQLParam("playerId") long playerId, @SQLParam("playerName") String playerName, @SQLParam("accountName") String accountName,
			@SQLParam("regBeginTime") int regBeginTime, @SQLParam("regEndTime") int regEndTime, @SQLParam("loginBeginTime") int loginBeginTime,
			@SQLParam("loginEndTime") int loginEndTime);

	@SQL("SELECT concat(SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-300 or offLineTime<=lastLogin THEN 1 ELSE 0 END),'|||',SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-600 or offLineTime<=lastLogin THEN 1 ELSE 0 END),'|||',SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-3600 or offLineTime<=lastLogin THEN 1 ELSE 0 END)) FROM kf_player where offLineTime>=UNIX_TIMESTAMP()-3600 or offLineTime<=lastLogin")
	public String getOnlineCounts();

	@SQL("SELECT concat(SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-300 or offLineTime<=lastLogin THEN 1 ELSE 0 END),'|||',SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-600 or offLineTime<=lastLogin THEN 1 ELSE 0 END),'|||',SUM(CASE WHEN offLineTime>=UNIX_TIMESTAMP()-3600 or offLineTime<=lastLogin THEN 1 ELSE 0 END)) FROM kf_player where (offLineTime>=UNIX_TIMESTAMP()-3600 or offLineTime<=lastLogin) and regTime < UNIX_TIMESTAMP()-86400")
	public String getOnlineFilterCounts();
	
	@SQL("select platform,count(playerId) from "+TABLE+" where offLineTime>=UNIX_TIMESTAMP()-:1 Or offLineTime<=lastLogin group by platform ")
	@KeyColumnOfMap("platform")
	public Map<String, Integer> getOnLine(int scends);
}
